import 'package:eplorex/pages/my/profile/ButtonWidget.dart';
import 'package:eplorex/utils/HttpUtil.dart';
import 'package:flutter/material.dart';

import '../../../utils/ColorUtil.dart';

class EditUserName extends StatelessWidget {
   EditUserName(String userName, {super.key});
  var formKey = GlobalKey<FormState>();
  var  userName;


   void _submit(BuildContext context) {
     if (formKey.currentState!.validate()) {
       formKey.currentState!.save();
       HttpUtil.getInstance()
           .post('/api/user/v1/my/profile/edit/name',
           data:userName)
           .then((value) {

       });
     }
   }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        leading:  ButtonWidget(
          width: 80,
          height: 50,
          onPressed: () {
            Navigator.pop(context);
          },
          highlightedColor: ColorUtil.hexColor(0xf7f7f7),
          bgColor: ColorUtil.hexColor(0xffffff),
          bgHighlightedColor: ColorUtil.hexColor(0xf7f7f7),
          enabled: true,
          bgDisableColor: Colors.grey,
          borderRadius: 8.0,
          child: Text(
            "取消",
            textAlign: TextAlign.center,
            softWrap: true,
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.w500,
              fontStyle: FontStyle.normal,
              color: ColorUtil.hexColor(0xA1A1A1),
              decoration: TextDecoration.none,
            ),
          ),
        ),
        title: Text("编辑名字"),actions: [
        ButtonWidget(
          width: 80,
          height: 50,
          onPressed: () {
            Navigator.pop(context);
            _submit(context);
          },
          highlightedColor: ColorUtil.hexColor(0xf7f7f7),
          bgColor: ColorUtil.hexColor(0xffffff),
          bgHighlightedColor: ColorUtil.hexColor(0xf7f7f7),
          enabled: true,
          bgDisableColor: Colors.grey,
          borderRadius: 8.0,
          child: Text(
            "确定",
            textAlign: TextAlign.center,
            softWrap: true,
            style: TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.w400,
              fontStyle: FontStyle.normal,
              color: ColorUtil.hexColor(0x338FFF),
              decoration: TextDecoration.none,
            ),
          ),
        )
      ],),
      body: Container(
        child:   Form(
          key: formKey,
          child: Column(
            // crossAxisAlignment: CrossAxisAlignment.c,
            children: [
              Text("1个月内只可修改一次"),
              Padding(padding: EdgeInsets.symmetric(horizontal: 20),child: TextFormField(
                maxLength: 15,
                decoration: InputDecoration(
                  filled: true,
                  fillColor: Colors.white,
                  hintText: userName ?? '输入新名字',
                  contentPadding:
                  EdgeInsets.symmetric(vertical: 4, horizontal: 8),
                  border: OutlineInputBorder(
                    gapPadding: 0,
                    borderSide: BorderSide.none,
                    borderRadius: BorderRadius.all(Radius.circular(10)),
                  ),
                ),
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return '输入新名字';
                  }
                  return null;
                },
                onSaved: (value) => userName = value!,
              ),)
            ],
          ),
        ),
      ),
    );
  }
}
